package cn.wolfcode.luowowo.article.service.impl;

import cn.wolfcode.luowowo.article.domain.Destination;
import cn.wolfcode.luowowo.article.domain.StrategyDetail;
import cn.wolfcode.luowowo.article.domain.Travel;
import cn.wolfcode.luowowo.article.domain.TravelContent;
import cn.wolfcode.luowowo.article.mapper.TravelContentMapper;
import cn.wolfcode.luowowo.article.mapper.TravelMapper;
import cn.wolfcode.luowowo.article.service.IDestinationService;
import cn.wolfcode.luowowo.article.service.ITravelService;
import cn.wolfcode.luowowo.common.query.TravelQueryObject;
import cn.wolfcode.luowowo.common.util.AjaxResult;
import cn.wolfcode.luowowo.member.domain.UserInfo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.dubbo.config.annotation.Reference;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.List;

@Service
public class TravelServiceImpl implements ITravelService {

    @Autowired
    private TravelMapper travelMapper;
    @Autowired
    private TravelContentMapper travelContentMapper;
    @Reference
    private IDestinationService destinationService;

    public PageInfo query(TravelQueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize(),qo.getOrderBy());
        List<Travel> list = travelMapper.selectForList(qo);
        return new PageInfo(list);
    }

    public Travel selectById(Long travelId) {
        Travel travel = travelMapper.selectByPrimaryKey(travelId);
        TravelContent content = travelContentMapper.selectByPrimaryKey(travelId);
        travel.setTravelContent(content);
        return travel;
    }

    public List<StrategyDetail> listViewnumTop3(Long destId) {
        return travelMapper.listViewnumTop3(destId);
    }

    public AjaxResult savaOrUpdate(Travel travel) {
        AjaxResult result = new AjaxResult();
        //补充摘要
        travel.setSummary("AI自动生成中...");
        //设置最后更新时间
        travel.setLastUpdateTime(new Date());
        //获取文章内容
        TravelContent content = travel.getTravelContent();

        if (travel.getId() == null) {
            //设置发布时间 (这里默认是发布)
            travel.setReleaseTime(new Date());
            //设置创建时间
            travel.setCreateTime(new Date());
            travelMapper.insert(travel);
            //保存文章
            content.setId(travel.getId());
            travelContentMapper.insert(content);
        }else {
            travelMapper.updateByPrimaryKey(travel);
            content.setId(travel.getId());
            travelContentMapper.updateByPrimaryKey(content);
        }
        result.setData(travel.getId());
        return result;
    }

    public List<Travel> list() {
        return travelMapper.selectAll();
    }

    public PageInfo<Travel> queryforUid(TravelQueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize());
        List<Travel> list = travelMapper.queryForUid(qo);
        return new  PageInfo<>(list);
    }

    public Integer getTraveltotalByUid(Long uid) {
        Integer num = travelMapper.getTraveltotalByUid(uid);
        //如果查询数量为空就为0
        if (num != null) {
            return  num;
        } else {
            return 0;
        }
    }

    public Integer getReplynumtotalByUid(Long uid) {
        Integer num = travelMapper.getReplynumtotalByUid(uid);
        //如果查询数量为空就为0
        if (num != null) {
            return  num;
        } else {
            return 0;
        }
    }

    public Integer getViewnumtotalByUid(Long uid) {
        Integer num = travelMapper.getViewnumtotalByUid(uid);
        //如果查询数量为空就为0
        if (num != null) {
            return  num;
        } else {
            return 0;
        }
    }

    @Override
    public List<Travel> getTravelsById(Long uid) {
        List<Travel> travels=travelMapper.getTravelsById(uid);
        for (Travel travel : travels) {
            Long destId = travel.getDest().getId();
            List<Destination> toasts = destinationService.getToasts(destId);
            travel.setCountry(toasts.get(0));
        }
        return travels;
    }

    @Override
    public void updateTravelStats(Travel travel) {
        travelMapper.updateTravelStats(travel);
    }
}
